<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="仪器设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入仪器设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪器类别" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择仪器类别">
          <el-option v-for="dict in dict.type.hj_instrument_category" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪器序列号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入仪器序列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购买日期" prop="purchaseDate">
        <el-date-picker clearable
          v-model="queryParams.purchaseDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择购买日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="放置位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入放置位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['instrumentation:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['instrumentation:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['instrumentation:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['instrumentation:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="仪器设备名称" align="center" prop="name" />

      <el-table-column label="仪器设备照片" align="center" prop="imageUrl" >
       <template slot-scope="scope">  
          <el-image  
          style="width: 80px; height: 50px"  
            :src="scope.row.imageUrl"  
            ref="imageId"  
          ></el-image>  
        </template>  
      </el-table-column>

      <el-table-column label="仪器类别" align="center" prop="category" width="130">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hj_instrument_category" :value="scope.row.category"/>
        </template>
      </el-table-column>

      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="仪器序列号" align="center" prop="serialNumber" />
      <el-table-column label="购买日期" align="center" prop="purchaseDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hj_instrument_status" :value="scope.row.status"/>
        </template>
      </el-table-column>

      <el-table-column label="放置位置" align="center" prop="location" />
      <el-table-column label="备注">
        <template slot-scope="scope">
          <!-- 这里是备注列，每行的备注显示为“查看”按钮 -->
          <el-button size="small" @click="viewDescription(scope.row)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['instrumentation:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['instrumentation:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仪器设备信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="仪器图片" prop="imageUrl">
          <ImageUpload v-model="form.imageUrl"  />
        </el-form-item>

        <el-form-item label="仪器名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入仪器设备名称" />
        </el-form-item>

        <el-form-item label="仪器类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择仪器类别">
            <el-option v-for="dict in dict.type.hj_instrument_category" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="仪器序列号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入仪器序列号" />
        </el-form-item>
        <el-form-item label="购买日期" prop="purchaseDate">
          <el-date-picker clearable
            v-model="form.purchaseDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择购买日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="放置位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入放置位置" />
        </el-form-item>

        <el-form-item label="状态" v-show="statusVisible">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.hj_instrument_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 备注弹窗部分 -->
    <el-dialog title="备注内容" :visible.sync="dialogVisible" width="50%">
      <p>{{ currentDescription }}</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/instrumentation/info";
import store from "@/store";

export default {
  name: "Info",
  dicts: ['hj_instrument_category','hj_instrument_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仪器设备信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        category: null,
        brand: null,
        model: null,
        serialNumber: null,
        purchaseDate: null,
        status: null,
        location: null,
        imageUrl: null,
        description: null,
        deleteBy: null,
        deleteTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        imageUrl: [
          { required: true, message: "仪器设备图片不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "仪器设备名称不能为空", trigger: "blur" }
        ],
        category: [
          { required: true, message: "仪器设备类别不能为空", trigger: "blur" }
        ],
        brand: [
          { required: true, message: "仪器设备品牌不能为空", trigger: "blur" }
        ],
        model: [
          { required: true, message: "仪器设备型号不能为空", trigger: "blur" }
        ],
        serialNumber: [
          { required: true, message: "仪器设备序列号不能为空", trigger: "blur" }
        ],
        purchaseDate: [
          { required: true, message: "仪器设备购买日期不能为空", trigger: "blur" }
        ],
        location: [
          { required: true, message: "仪器设备放置位置不能为空", trigger: "blur" }
        ],
      },
      dialogVisible: false, // 控制弹窗显示与否
      currentDescription: '', // 当前显示的备注内容
      //状态选项，默认隐藏
      statusVisible: false, 
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询仪器设备信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.statusVisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        instrumentId: null,
        name: null,
        category: null,
        brand: null,
        model: null,
        serialNumber: null,
        purchaseDate: null,
        status: null,
        location: null,
        imageUrl: null,
        description: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deleteBy: null,
        deleteTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.instrumentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仪器设备信息";
      this.statusVisible = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const instrumentId = row.instrumentId || this.ids
      getInfo(instrumentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仪器设备信息";
        this.statusVisible = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.instrumentId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const instrumentIds = row.instrumentId || this.ids;
      this.$modal.confirm('是否确认删除仪器设备信息编号为"' + instrumentIds + '"的数据项？').then(function() {
        return delInfo(instrumentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('instrumentation/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    // 点击“查看”按钮时调用此方法，显示对应的备注内容
    viewDescription(row) {
      this.currentDescription = row.description;  // 获取当前行的备注内容
      this.dialogVisible = true; // 显示弹窗
    }
  }
};
</script>
